import { join } from 'path'
import { LoggerOptions, format, transports } from 'winston'
import { ViteConfig } from 'koacat'
import tailwindcss from 'tailwindcss'
import postcssImport from 'postcss-import'
import path from 'path'
import { tailwindColorConfig, cssColorConfig } from '../src/shared/theme/css_theme'
import { fileURLToPath } from 'url'

export type Env = 'production' | 'local' | 'unittest' | 'build'

export interface Config {
    ENV: Env,
    APP_NAME: string,
    JWT_KEY: string,
    LISTEN_PORT: number,
    HTTPS_LISTEN_PORT: number,
    WEBSOCKET_PORT: number,
    TIMEZONE: string,
    NAS_PATH: string,
    LOG_PATH: string,
    CPUS: number,
    VITEX: ViteConfig,
    // REDIS: {
    //     ignore: boolean,
    //     port: number,
    //     host: string,
    //     password: string,
    //     db: number
    // },
    // INFLUX: {
    //     ignore: boolean,
    //     options: ClientOptions,
    //     org: string
    // },
    MONGODB: {
        ignore: boolean,
        uri: string
    },
    // FEISHU_ROBOTS: Record<string, { url: string, color: string }>,
    LOGGER: LoggerOptions,
    GEN_API_FLOW?: boolean,
    // ALIYUN: {
    //     ossRegion: string,
    //     ossBucket: string,
    //     ossDefaultFolder: string,
    //     accessId: string,
    //     accessKey: string
    // },
    // AGORA: {
    //     appId: string,
    //     cert: string,
    //     djiChannel: string
    // },
    SSL: {
        cert: string,
        key: string
    }
}

export function getDefaultConfig(): Config {
  const config = {} as Config

  config.ENV = process.env.NODE_ENV as Env || 'unittest'
  config.APP_NAME = 'power-ops'
  config.JWT_KEY = process.env.JWT_KEY || 'JWT_KEY_123456'

  config.LISTEN_PORT = process.env.LISTEN_PORT ? parseInt(process.env.LISTEN_PORT) : 80
  config.HTTPS_LISTEN_PORT = process.env.HTTPS_LISTEN_PORT ? parseInt(process.env.HTTPS_LISTEN_PORT) : 443
  config.WEBSOCKET_PORT = process.env.WEBSOCKET_PORT ? parseInt(process.env.WEBSOCKET_PORT) : 81
  config.TIMEZONE = 'asia/shanghai'
  config.CPUS = process.env.CPUS ? parseInt(process.env.CPUS) : 2

  config.NAS_PATH = process.env.NAS_PATH || 'temp'
  config.LOG_PATH = join(config.NAS_PATH, 'log', config.APP_NAME)

  const clientDir = 'build/client'
  const serverDir = 'build/server'
  config.VITEX = {
    clientDir,
    serverDir,
    customHead: `<style>
    .n-button { background-color: var(--n-color)!important; } ${cssColorConfig}
    html, body, #app, #body { background-color: var(--secondary-500); color: var(--char-500); }
    @keyframes infinite-scale {
      0% { transform: scale(1); }
      50% { transform: scale(1.4); }
      100% { transform: scale(1); }
    }
    .infinite-scale { animation: infinite-scale 1s infinite; }
    </style>`,
    vite: {
      resolve: { alias: { '@': path.resolve(path.dirname(fileURLToPath(import.meta.url)), 'src') } },
      publicDir: false,
      base: '/vite/',
      css: {
        postcss: {
          plugins: [
            postcssImport,
            tailwindcss({
              content: ['./src/**/*.vue'],
              theme: {
                extend: {
                  colors: tailwindColorConfig,
                }
              }
            })
          ]
        }
      }
    },
    viteClient: {
      build: {
        outDir: clientDir,
        rollupOptions: {
          input: [],
          output: { format: 'esm' }
        }
      }
    },
    viteServer: {
      build: {
        outDir: serverDir,
        rollupOptions: {
          input: [],
          output: { format: 'cjs' }
        }
      }
    }
  }

  // config.REDIS = {
  //     ignore: false,
  //     port: process.env.REDIS_PORT ? parseInt(process.env.REDIS_PORT) : 1004,
  //     host: process.env.REDIS_HOST || '127.0.0.1',
  //     password: process.env.REDIS_PASSWORD || 'pass',
  //     db: 0
  // }

  // 记得修改地址
  config.MONGODB = {
    ignore: true,
    uri: process.env.MONGODB_URI || 'mongodb://admin:admin@127.0.0.1:2101'//,localhost:2102,localhost:2103'
  }

  // config.INFLUX = {
  //     ignore: false,
  //     options: {
  //         url: process.env.INFLUX_URL || 'http://8.134.154.108:1005',
  //         token: process.env.INFLUX_TOKEN || 'TYh1VUJhvo1x53Zhs7n12Lmq1zoG8y-u8lXRYkP6YZPJu92zD38jKw2tVL4LDnHuVh-EuDLO6dqREDxmttrgFQ=='
  //     },
  //     org: 'org'
  // }
  //
  // config.FEISHU_ROBOTS = {
  //     alert: {
  //         url: 'https://open.feishu.cn/open-apis/bot/v2/hook/aba376b1-0ea8-48fb-957c-edb09bb1812b',
  //         color: 'red'
  //     },
  //     aircraft: {
  //         url: 'https://open.feishu.cn/open-apis/bot/v2/hook/aba376b1-0ea8-48fb-957c-edb09bb1812b',
  //         color: 'purple'
  //     }
  // }

  config.LOGGER = {
    level: 'debug',
    exitOnError: false,
    format: format.combine(
      format.timestamp(),
      format.printf(({ level, message, timestamp }) => `${timestamp} [${level}] ${message}`)
    ),
    transports: [
      new transports.File({
        filename: join(config.LOG_PATH, 'app.log'),
        tailable: true,
        zippedArchive: false,
        maxFiles: 50
      })
      // new DailyRotateFile({
      //   filename: join(config.LOG_PATH, 'app-%DATE%.log'),
      //   datePattern: 'YYYY-MM-DD-HH',
      //   zippedArchive: false,
      //   maxSize: '20m',
      //   maxFiles: '15d'
      // })
    ]
  }

  // config.ALIYUN = {
  //     ossRegion: process.env.OSS_REGION || 'oss-cn-guangzhou',
  //     ossBucket: process.env.OSS_BUCKET || 'nofee-default-bucket',
  //     accessId: process.env.ALI_ACCESS_ID || 'LTAI5tQ3dzfKXUnqW6XA6Yvb',
  //     accessKey: process.env.ALI_ACCESS_KEY || 'yAwyTpEQp7M0ZybMWrFeVuQm56RidM',
  //     ossDefaultFolder: process.env.OSS_DEFAULT_FOLDER || 'files'
  // }
  //
  // config.AGORA = {
  //     appId: process.env.AGORA_APPID || 'c0a7a13e2c0148598662f4646376bc27',
  //     cert: process.env.AGORA_CERT || '58c302e4ce2e4a05bdbc24a67ec6b5aa',
  //     djiChannel: process.env.AGORA_DJI_CHANNEL || 'dji'
  // }

  config.SSL = {
    cert: process.env.SSL_CERT || '/opt/https/https.cert',
    key: process.env.SSL_KEY || '/opt/https/https.key'
  }

  return config
}